/**
 * Created by lx on 2017/5/1.
 */
layui.use(['form'], function () {
    var form = layui.form();
    var business = {};
    $(document).ready(function () {
        if (window.location.hash != null && window.location.hash != "") {
            var has = window.location.hash;
            has = has.replace("#", "");
            has = has.replace(/\'/g, "\"");
            business = JSON.parse(has);
            if(isNotNull(business.id)){
                $.ajax({
                    type: "POST",
                    url: "/business/listFlow",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(business),
                    dataType: "json",
                    success: function (data) {
                        if (data.success) {
                            var content = '' ;
                            $.each(data.param.list,function(i,val){
                                $('#flowList').append(createFlowItem({id:val.id,name:val.name,description:val.description})) ;
                            }) ;
                            form.render() ;
                        } else {
                            layer.msg("获取步骤列表错误");
                        }
                    },
                    error: function (message) {
                        layer.msg("获取步骤列表错误");
                    }
                });
            }
        }
        autoInject(business);
        form.render();

        form.on('submit(yes)', function (data) {
            var url;
            if (business.id == undefined) {
                url = "/business/add"
            } else {
                url = "/business/update";
            }
            business = autoFetch(business);
            delete business.order ;
            business.businessFlowDOs = [] ;
            $('#flowList .layui-form-item').each(function(index,element){
                if(isNotNull(element.id)){
                    business.businessFlowDOs.push({flowId:element.id,flowOrder:index}) ;
                }
            }) ;
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(business),
                dataType: "json",
                success: function (data) {
                    if (data.success) {
                        layer.msg("新增业务成功");
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    } else {
                        layer.msg("新增业务失败");
                    }
                },
                error: function (message) {
                    layer.msg("新增业务失败");
                }
            });
            return false;
        });

        var isAdd = false ;
        var isUpdate = false ;

        form.on('submit(addFlow)', function (data) {
            isAdd = true ;
            $('#mid').show() ;
            $('#midden').show();
            $('#flowOrder').show() ;
            $('#head').hide() ;
            $('#bottom').hide() ;
            $('#list').hide() ;
            $.ajax({
                type: "POST",
                url: "/flow/list?pageNum=1&pageSize=1000",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    if (data.success) {
                        $('#updateFlow').empty() ;
                        $('#updateFlow').append(createFlowList(data.param)) ;
                        $('#updateFlow').append()
                        $('#flowList .layui-form-item').each(function(index,item){
                            if(isNotNull(item.id)){
                                $('#updateFlow option[value="'+item.id+'"]').remove() ;
                            }
                        }) ;
                        form.render() ;
                    } else {
                        layer.msg("获取步骤列表错误");
                    }
                },
                error: function (message) {
                    layer.msg("获取步骤列表错误");
                }
            });
            return false;
        });

        form.on('submit(deleteFlow)', function (data) {
            $('#mid').show() ;
            $('#midden').show();
            $('#list').hide() ;
            $('#head').hide() ;
            $('#bottom').hide() ;
            $('#flowOrder').hide() ;
            $('#updateFlow').empty() ;
            var param = {list:[]} ;
            $('#flowList .layui-form-item').each(function(index,element){
                if(isNotNull(element.id)){
                    var name = $('#'+element.id+' label').text() ;
                    var description = $('#'+element.id+' input').val() ;
                    param.list.push({id:element.id,name:name,description:description}) ;
                }
            }) ;
            $('#updateFlow').append(createFlowList(param)) ;
            form.render() ;
            return false;
        });

        form.on('submit(updateFlow)', function (data) {
            $('#mid').show() ;
            $('#midden').show();
            $('#list').hide() ;
            $('#head').hide() ;
            $('#bottom').hide() ;
            $('#flowOrder').show() ;
            $('#updateFlow').empty() ;
            var param = {list:[]} ;
            $('#flowList .layui-form-item').each(function(index,element){
                if(isNotNull(element.id)){
                    var name = $('#'+element.id+' label').text() ;
                    var description = $('#'+element.id+' input').val() ;
                    param.list.push({id:element.id,name:name,description:description}) ;
                }
            }) ;
            $('#updateFlow').append(createFlowList(param)) ;
            isUpdate = true ;
            isAdd = true ;
            form.render() ;
            return false;
        });


        form.on('submit(yesFlow)', function (data) {
            $('#mid').hide() ;
            $('#midden').hide();
            $('#list').show() ;
            $('#head').show() ;
            $('#bottom').show() ;
            var val = $('#updateFlow .layui-this').attr('lay-value') ;
            if(isAdd){
                if(isNotNull(val)){
                    if(isUpdate){
                        $('#'+val).remove() ;
                    }
                    var text= $('#updateFlow .layui-this').text() ;
                    var order = $('#order').val() - 1 ;
                    var flowList = $('#flowList .layui-form-item') ;
                    if(flowList.length==0 || flowList.length<=order){
                        $('#flowList').append(createFlowItem({id:val,name:text.split(':')[0],description:text.split(':')[1]})) ;
                    } else if(order<1){
                        $('#flowList').prepend(createFlowItem({id:val,name:text.split(':')[0],description:text.split(':')[1]})) ;
                    } else {
                       $('#flowList .layui-form-item').each(function(index,element){
                           if(index==order){
                               $('#'+element.id).before(createFlowItem({id:val,name:text.split(':')[0],description:text.split(':')[1]})) ;
                           }
                       }) ;
                    }
                }
                isAdd = false ;
                isUpdate = false ;
            }else {
                $('#'+val).remove() ;
            }
            form.render() ;
            return false;
        });

        form.on('submit(cancelFlow)', function (data) {
            $('#mid').hide() ;
            $('#midden').hide();
            $('#list').show() ;
            $('#head').show() ;
            $('#bottom').show() ;
            isAdd = false ;
            isUpdate = false ;
            return false;
        });

        $('#cancel').click(function () {
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
            return false;
        });
    });
})
;